#!/usr/bin/env python
#coding=utf-8
'''
Created on Jul 22, 2010

@author: lxd
'''
import wx
from common.tools import Tools
import threading
import tproxy
from common.web import Web

class GetProxyThread(threading.Thread):        
    def __init__(self, window):
        threading.Thread.__init__(self)
        self.timeToQuit = threading.Event()
        self.timeToQuit.clear()
        self.window = window
        
    def stop(self):
        self.timeToQuit.set()
          
    def run(self):
        tproxyWeb = tproxy.TproxyWeb()
        f = tproxyWeb.getMain()
        proxy_list = tproxy.parseProxy(f)
        wx.CallAfter(self.window.getProxyCall, proxy_list)
        
class GetIpThread(threading.Thread):        
    def __init__(self, window, proxy):
        threading.Thread.__init__(self)
        self.timeToQuit = threading.Event()
        self.timeToQuit.clear()
        self.window = window
        self.proxy = proxy
        
    def stop(self):
        self.timeToQuit.set()
          
    def run(self):
        web = Web(proxy=self.proxy, times=1)
        f = web.fetchData('', 'http://www.ip138.com/ip2city.asp')
        wx.CallAfter(self.window.getIpCall, f, self.proxy)
        
class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, title=u'代理地址', style=wx.DEFAULT_FRAME_STYLE)        
        self.palMain = wx.Panel(self)                
        self.lblTop = self.createTop() 
        self.lblCount = wx.StaticText(self.palMain, -1, u"共0个代理地址")       
        self.lstMessage = wx.ListBox(self.palMain, -1, size=(500, 600), style=wx.LB_SINGLE)   
        self.layout() 
        self.proxy_list = self.loadProxy()
        GetProxyThread(self).start()
        self.Center() 
        self.Bind(wx.EVT_CLOSE, self.OnClose)

    def createTop(self):
        lblTop = wx.StaticText(self.palMain, -1, u"代理地址列表")
        lblTop.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
        return lblTop
   
    def layout(self):
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(self.lblTop, 0, wx.ALL, 5)
        mainSizer.Add(wx.StaticLine(self.palMain), 0, wx.EXPAND | wx.TOP | wx.BOTTOM, 5)
        mainSizer.Add(self.lblCount, 0, wx.ALL, 5)
        mainSizer.Add(self.lstMessage, 1, wx.EXPAND)
        self.palMain.SetSizer(mainSizer)
        mainSizer.Fit(self)
        mainSizer.SetSizeHints(self)
        
    def getProxyCall(self, proxy_list):
        if proxy_list:
            if self.proxy_list:
                p_list = set(proxy_list + self.proxy_list)
            else:
                p_list = set(proxy_list)
            self.lstMessage.Clear()
            for proxy in p_list:
                #todo应限制线程数
                GetIpThread(self, proxy).start()
        else:
            self.lblCount.SetLabel(u'获取代理地址失败')
            
    def getIpCall(self, f, proxy):
        if tproxy.getIp(f): 
            self.lstMessage.Append(proxy)
        count = self.lstMessage.GetCount()
        self.lblCount.SetLabel(u'共%s个代理地址' % count)   
          
    def OnClose(self, event):
        new_proxy = self.lstMessage.GetItems()
        proxy = Tools.loadData(r'db/proxy') 
        if new_proxy:       
            if (proxy and new_proxy != proxy) or not proxy: 
                dlg = wx.MessageDialog(None, u'代理地址已变化，要保存吗？', u'友情提示', wx.YES_NO | wx.CANCEL | wx.ICON_QUESTION)
                retCode = dlg.ShowModal()    
                if retCode == wx.ID_YES:
                    Tools.saveData(r'db/proxy', new_proxy)
                    self.Destroy()
                elif retCode == wx.ID_NO:
                    self.Destroy()                    
            else:
                self.Destroy()                    
        else:
            self.Destroy()
            
    def loadProxy(self):
        proxy = Tools.loadData(r'db/proxy')
        if proxy:
            for p in proxy:
                self.lstMessage.Append(p)
        return proxy    
                       
if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()


